/*******************************************************************************
 * This file is part of Crunch Network.
 *
 * Crunch Network is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Crunch Network is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * Crunch Network.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package com.crunch.network;

/**
 * Used to provide settings to network systems.
 */
public class NetworkSettings {
	/**
	 * The maximum size a single packet is allowed to be.
	 */
	public int maxPacketSize;

	/**
	 * How much to smooth ping time when computing network statistics. Value must be between 0 and 1, where 1 disables
	 * smoothing.
	 */
	public float pingSmoothing;

	/**
	 * User-defined callbacks to be triggered for certain network events.
	 */
	public NetworkCallbacks callbacks;

	/**
	 * The port on which data is received.
	 */
	public int port;

	/**
	 * An ID to identify the protocol for this set of connections.
	 */
	public int protocolId;

	/**
	 * Amount of time a connection is allows to stay idle (i.e. receive no data) before it is destroyed.
	 */
	public int connectionTimeoutMs;

	/**
	 * The number of bits to use in message sequence numbers.
	 */
	public int messageSequenceNumberBits;

	/**
	 * The maximum number of concurrent local ghosts to support per connection.
	 */
	public int maxLocalGhosts;

	/**
	 * The maximum number of concurrent remote ghosts to support per connection.
	 */
	public int maxRemoteGhosts;

	/**
	 * The default constructor assigns default setting values.
	 */
	public NetworkSettings() {
		maxPacketSize = 1024; // 1kb
		pingSmoothing = 0.1f;
		callbacks = null;
		port = -1;
		protocolId = 0;
		connectionTimeoutMs = 10 * 1000; // 10 seconds
		messageSequenceNumberBits = 16;
		maxLocalGhosts = 0;
		maxRemoteGhosts = 0;
	}

	/**
	 * Copies the settings provided in {@code other}.
	 *
	 * @param   other the settings to copy.
	 */
	public NetworkSettings(NetworkSettings other) {
		maxPacketSize = other.maxPacketSize;
		pingSmoothing = other.pingSmoothing;
		callbacks = other.callbacks;
		port = other.port;
		protocolId = other.protocolId;
		connectionTimeoutMs = other.connectionTimeoutMs;
		messageSequenceNumberBits = other.messageSequenceNumberBits;
		maxLocalGhosts = other.maxLocalGhosts;
		maxRemoteGhosts = other.maxRemoteGhosts;
	}

	/**
	 * Verifies that all settings are valid.
	 *
	 * @return  whether all settings are valid.
	 */
	public boolean checkSettings() {
		return (maxPacketSize >= 32) &&
				(pingSmoothing > 0.0f && pingSmoothing <= 1.0f) &&
				(callbacks != null) &&
				(port >= 0 && port <= 65535) &&
				(connectionTimeoutMs > 0) &&
				(messageSequenceNumberBits > 0 && messageSequenceNumberBits < 32) &&
				(maxLocalGhosts >= 0) &&
				(maxRemoteGhosts >= 0);
	}
}
